function y = rand_bin(n, p, k, seed )
% Random Numbers from Binomial Distribution
%      Call:   function y = rand_bin( n, p, k, [seed] )
% Arguments  
%      Inputs: k - number of random numbers.
%              n - number of experiments (sample size).
%              p - probability of "success" in binomial experiment.
%           seed - seed for built in random number generator.
%                  Default seed = floor(sum(100*clock)).
%      Output: y - random number (vector) from Bin(n,p).
%  Examples of use:  pro = rand_bin(2, 10, 0.6);
%                    hist(rand_bin(20000, 20, 0.4), 210 )
% -----------------------------------------------------------------
if nargin < 4
    seed = floor(sum(100*clock)); %if not specified, seed different each time
end
   rand('state',seed);
%-------------------------------------
if(  n >= 1 & p >= 0 & p <= 1 & k > 0)
   vec = rand(n, round(k));
   y = sum( (vec > 1-p) );  %Paul Changed this to 1-p instead of 0.5
elseif (p > 1 | p < 0)
        error('The probability p is out of range.');
elseif ( round(k) < 1 )
       y = [ ];
       warning('The nubmer of realizations should be a positive integer.')
else
       error('Argument n should be a positive integer.');
end
%-------------------------------------------------------------------
% Copyright (C) 2002 by GaTech
% Ver 0.1   8/28/2002